<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<title>应急信息发布区域管理地图</title>
	<style type="text/css">
		body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
		#result {width:100%;font-size:12px;}
	</style>
	<script src="../../layui/layui.js"></script>
	<script src="../../js/util.js"></script>
	<link rel="stylesheet" type="text/css" href="../../css/mapstyle.css">
	<link rel="stylesheet" type="text/css" href="../../css/xcConfirm.css"/>
	<script src="../../js/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../js/xcConfirm.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=nSGoPNzNrehEaGBst1TZzklH"></script>
	<!--加载鼠标绘制工具-->
	<script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
	<link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />

</head>
<body>
	<div id="allmap"></div>
	<div id="loadingDiv" class="load-white-bg hide">
	<span class="qloading">正在加载...</span>
	</div> 
	<!-- <input type="button" value="获取绘制的覆盖物个数" onclick="alert(overlays.length)"/>
	<input type="button" value="清除所有覆盖物" onclick="clearAll()"/>
	<input type="button" value="保存" onclick="onSave()"/> -->
</body>
</html>
<script type="text/javascript">

var map = null;
var cityId;
var resultRegionValue;//结果区域图形值 1: a,b   2:经度，纬度#半径   3:4: 经度，纬度#经度，纬度
var resultRegionType;//结果区域图形 1行政区域组合 2圆形 3矩形 4 多边形

var colorData = [];
var lnglatNames = "";
var subPoints = [];
var pointsName = [];
var colorIndex = 0;
var initialFunc = function(){
    // 百度地图API功能
	map = new BMap.Map("allmap",{minZoom:8,maxZoom:18}); // 创建Map实例,设置地图允许的最小/大级别
	if(isHaveJW!=""){
		map.centerAndZoom(isHaveJW,13); // 初始化地图,设置中心点坐标和地图级别
	}else{
		map.centerAndZoom(cityJW,13); // 初始化地图,设置中心点坐标和地图级别
	}
	map.enableScrollWheelZoom(true);//开启鼠标滚轮缩放 
}
var markers = [];//全省基站点
var cityJW ;
var isHaveJW="";//获取是否已有圈画过的图形，如有则定位到图形
var specialFlag = "";
var num = 0;
 function getUserFlag(){
             $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        user=json;
                        console.info(json);
                        if(json.specialFlag == 1){
                        	specialFlag = 1;
                        }
                        }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            }
function getXuYiStation(){
	var XuYiStations = [];
	var url = "/smsArea/findXuYiStations";
	$.ajax({	
	 async : true,
				url : url,
				type : "post",
				success : function(data) {
					if (data != null) {
						XuYiStations  = data.data;
						loadStations(XuYiStations);
					} else {
						layer.msg("获取基站数据失败,请联系管理员处理!");
					}
				},
				error : function(data) {
					layer.msg('系统异常，请联系管理员处理!');
				}
			}); 
}
//加载盱眙基站
function loadStations(data){
	if (document.createElement('canvas').getContext) {  // 判断当前浏览器是否支持绘制海量点
        var XuYiStations = [];  // 添加海量点数据
        // debugger;
        for (var i = 0; i < data.length; i++) {
          XuYiStations.push(new BMap.Point(data[i].lng, data[i].lat));
        }
        
        var XuYioptions = {
            size: 4,
            shape: 2,
            color: '#d340c3'
        }
        var pointCollection = new BMap.PointCollection(XuYiStations, XuYioptions);  // 初始化PointCollection
        /* pointCollection.addEventListener('click', function (e) {
          alert('单击点的坐标为：' + e.point.lng + ',' + e.point.lat);  // 监听点击事件
        }); */
        map.addOverlay(pointCollection);  // 添加Overlay
    } else {
        alert('请在chrome、safari、IE8+以上浏览器查看本页面');
    }
    }
    
function findLngLatNames() {
			for(var i = 0; i < subPoints.length; i++){
						
						//var point = new BMap.Point(lng,lat);
						const marker = new BMap.Marker(subPoints[i]); // 创建标注  
						map.addOverlay(marker);
						//marker.addEventListener("click",showInfoP)
						//var pointIndex = getPointName();
						
						var nameLength = pointsName[i].length;
						const label = new BMap.Label(pointsName[i], {
							offset : new BMap.Size(2, -5)
						});
						label.setStyle({
							background : '#ff8355',
							border : '1px solid "#ff8355"',
							borderRadius : "5px",
							color : '#fff',
							height : "11px",
							lineHeight : "11px",
							textAlign : "center",
							width : nameLength * 15 - 70 + "px"
						});
						marker.setLabel(label); //为标注添加一个标签
						marker.setTitle(pointsName[i]);
						}
		}
    
//获取盱眙变电站数据
function getSubstation() {
			var url = "/smsArea/findXuYiSubStations?pathName=all&subStationName=all";
			var substationData=[];
			var subPathData=[];
			var num1;
			var signData=[];//需要标记的点位
			var signName=[];//标记点位的名称
			
			
			$.ajax({
				url : url,
				type : "post",
				success : function(data1) {
					if (data1 != 0) {
						subPathData=data1.data;
						for(let m= 0; m < subPathData.length;m++){
							substationData=subPathData[m];
							//substationData  = data1.data;
							  //num1= getNum();
							  //debugger;
						if (substationData.length > 0) {
							for (var i = 0; i < substationData.length; i++) {
								colorData.push(m);
								var points = [];
								var bdPoints = [];
								
								for(var j = 0; j < substationData[i].length; j++){
									//var point1 = new BMap.Point(substationData[i][j].lng,substationData[i][j].lat);
									var point1 = new BMap.Point(substationData[i][j].bdLng,substationData[i][j].bdLat);
									if(0===j){
										/* signName.push(substationData[i][j].pathName+"—"+substationData[i][j].name);
										signData.push(point1); */
										//lnglatNames = lnglatNames + substationData[i][j].lng+"|"+substationData[i][j].lat+"|"+substationData[i][j].pathName+"—"+substationData[i][j].name+","
										var point2 = new BMap.Point(substationData[i][j].bdLng,substationData[i][j].bdLat);
										subPoints.push(point2);
										pointsName.push(substationData[i][j].pathName+"—"+substationData[i][j].name);
									}
									points.push(point1);
								}

								//var convertor = new BMap.Convertor();
								for (var k = 0; k < points.length; k++) {
											//添加变电站标志
											addMarker(points[k]);
										}
										var colors = ['#FF204C','#2CFF08','#fa8722','#1753d0','#1fc5bb','#0ce84a','#ddec0d','#e816d7','#8d08ef','#f31096'];
										//var colors = ['#FFf000','#9900CC','#0000F','#008000','#FF00FF','#0ce84a','#000080','#008000','#00FFFF','#FF0000','#33CCCC','#0000F','#008000','#00FFFF','#FFf000','#993333','#FFFF00','#000099','#000066','#009933','#F00000','#FFCC00','#0099FF','#33CCCC','#FFCCFF','#33CC33','#CC0000','#3333CC','#CC0000','#3333CC','#993333'];
										var colorIndex1=getColorIndex();
										var colornow = colors[colorData[colorIndex1] % colors.length];
										var polygon = new BMap.Polygon(points,
												{
													strokeColor : colornow,
													fillColor : colornow,
													strokeWeight : 2,
													strokeOpacity : 1
												}); //创建多边形
										map.addOverlay(polygon);
							}
						}
						}
						//标记坐标转换

						findLngLatNames(lnglatNames)
						
					} else {
						layer.msg("获取变电站数据失败,请联系管理员处理!");
					}
				},
				error : function(data) {
					layer.msg('系统异常，请联系管理员处理!');
				}
			});
			
			
			}
			
// 编写自定义函数,创建标注
		function addMarker(point){
			var myIcon = new BMap.Icon("../../images/subStation.png", new BMap.Size(23, 25));
			var marker = new BMap.Marker(point, {icon: myIcon});
			map.addOverlay(marker);
			// var marker = new BMap.Marker(point);
			// map.addOverlay(marker);
			/* marker.addEventListener("click",getAttr);
			function getAttr(){
			var p = marker.getPosition();       //获取marker的位置
			console.info("marker的位置是" + point);
			} */
		}
function getNum(){
		return ++num;
}
//坐标转换完之后的回调函数
			function toBaidu(points){
			var bdPoints=[]; 
				        
			}
			
function getColorIndex(){
			return colorIndex++;
		}
//定位盱眙
function getBoundary(){       
		var bdary = new BMap.Boundary();
		bdary.get('盱眙', function(rs){       //获取行政区域
			//map.clearOverlays();        //清除地图覆盖物       
			var count = rs.boundaries.length; //行政区域的点有多少个
			if (count === 0) {
				alert('未能获取当前输入行政区域');
				return ;
			}
          	var pointArray = [];
			for (var i = 0; i < count; i++) {
				var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 3, strokeColor: "#000000",fillColor:"#ffffff",fillOpacity:0.01}); //建立多边形覆盖物
				//var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
				map.addOverlay(ply);  //添加覆盖物
				pointArray = pointArray.concat(ply.getPath());
			}  
						
			map.setViewport(pointArray);    //调整视野           
		});   
	}
	
// 编写自定义函数,创建标注
$(document).ready(function() {
getUserFlag();
if(specialFlag==1){
    	getBoundary();
    //getXuYiStation();
    getSubstation();
    }
    cityId=getUrlParam("cityId");
    resultRegionType=getUrlParam("regionType");
    resultRegionValue=getUrlParam("regionTypeValue");
    cityJW= getJWByCityId(cityId);//获取对应地市大大致经纬度
    isHaveJW = getJWIsHave(resultRegionValue);
    
        
    initialFunc();
    //getData();
    getTuxing();

});

function getJWIsHave(resultRegionValue){
	var array = resultRegionValue.split('+');
	if(array.length>1){
		var JW = array[0].split(',');
		var JD = JW[0];
		var WD = JW[1];
		return new BMap.Point(JD,WD);
	}else{
		return "";
	}
	
}

function getData(){
    var url="/smsArea/findAllStation?cityId="+getUrlParam("cityId")+"&countyId=&longitudeMin=&longitudeMax=&latitudeMin=&latitudeMax=";
    $.ajax({
        url:url,
        type:"post",
        success:function(data){
            $.each(data,function(i,d){
                markers[i]={position:{lat:d.latitude,lng:d.longitude},stationid:d.stationMarkId,stationname:d.stationMarkName};
            });
        }
    });
}
//获取图形
function getTuxing(){
	/* if(resultRegionType==2){
	    var arr = resultRegionValue.split('+');
	    var xy=arr[0].split(',');
		var c = new BMap.Point(xy[0],xy[1]); //圆心 
		var circle = new BMap.Circle(c, arr[1]);//测试圆
		map.clearOverlays();
		map.addOverlay(circle);
	}else if(resultRegionType==3){
		var arr = resultRegionValue.split('+');
		var xy1=arr[0].split(',');
		var xy2=arr[1].split(',');
		var xy3=arr[2].split(',');
		var xy4=arr[3].split(',');
		var pts = [];
	    //bds的四个脚点坐标
	    var leftTop = new BMap.Point(xy1[0], xy1[1]);
	    var rightTop = new BMap.Point(xy2[0], xy2[1]);
	    var leftBottom = new BMap.Point(xy3[0], xy3[1]);
	    var rightBottom = new BMap.Point(xy4[0], xy4[1]);
	    pts.push(leftTop);
	    pts.push(rightTop);
	    pts.push(rightBottom);
	    pts.push(leftBottom);
	    var rect = new BMap.Polygon(pts);
	    map.addOverlay(rect);//添加测试矩形   
	}else  */if(resultRegionType==4||resultRegionType==3){
	    var arr = resultRegionValue.split('+');
	    var pts = [];
	    for(var i=0;i<arr.length;i++){
	        var pt = new BMap.Point(arr[i].split(',')[0], arr[i].split(',')[1]);
	    	pts.push(pt);
	    }
	    
	    var ply = new BMap.Polygon(pts);
	    map.clearOverlays();
	    map.addOverlay(ply);  
	}else if(resultRegionType==2){
		var arr = resultRegionValue.split('+');
	    var xy=arr[0].split(',');
		var c = new BMap.Point(xy[0],xy[1]); //圆心 
		var circle = new BMap.Circle(c, arr[1]);//测试圆
		map.clearOverlays();
		map.addOverlay(circle);
	}

}



// 苏州   11淮安   12宿迁   13南京   14连云港   15徐州   16常州   17镇江   18无锡   19南通   20泰州   21 盐城   22 扬州   23
function getJWByCityId(cityId){
    if(cityId=="11"){
        return new BMap.Point(120.58,31.30);
    }else if(cityId=="12"){
        return new BMap.Point(119.02,33.62);
    }else if(cityId=="13"){
        return new BMap.Point(118.28,33.97);
    }else if(cityId=="14"){
        return new BMap.Point(118.78,32.04);
    }else if(cityId=="15"){
        return new BMap.Point(119.22,34.60);
    }else if(cityId=="16"){
        return new BMap.Point(117.18,34.27);
    }else if(cityId=="17"){
        return new BMap.Point(119.95,31.78);
    }else if(cityId=="18"){
        return new BMap.Point(119.45,32.20);
    }else if(cityId=="19"){
        return new BMap.Point(120.30,31.57);
    }else if(cityId=="20"){
        return new BMap.Point(120.88,31.98);
    }else if(cityId=="21"){
        return new BMap.Point(119.92,32.45);
    }else if(cityId=="22"){
        return new BMap.Point(120.15,33.35);
    }else if(cityId=="23"){
        return new BMap.Point(119.40,32.40);
    }else{
        return new BMap.Point(118.875, 32.825);
    }
}


</script>
